package com.ys.controller.web.storage;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.entity.hr.ThrOperateCompanyApply;
import com.ys.entity.sys.YsProcInst;
import com.ys.service.storage.TccDistributeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.storage.TccDistribute;
import com.ys.entity.storage.TccDistributeDetail;
import com.ys.enume.storage.TccDistributeEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/storage/tccDistribute")
public class WebTccDistributeController {
	
	@Autowired
	private CommonApiService<TccDistribute> commonApiService;
	@Autowired
	private TccDistributeService tccDistributeService;
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){

		try {
			String bid = commonApiService.getCommonNextBid(MenuId.TCC_Distribute);
			
			TccDistribute entity = new TccDistribute()
					.setBid(bid)
					.setType(TccDistributeEnum.Type.BY_HAND.value)
					.setCreateTime(new Date());
			
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCC_Distribute, new ArrayList<Map<String, Object>>());
			
			return commonApiService.toAdd(model, entity, MenuId.TCC_Distribute, null, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 审核
	 */
	@RequiresPermissions("tccDistribute:add")
	@GetMapping("/toFlow")
	public String toFlow(Model model, HttpServletRequest request, HttpServletResponse response,
			String bid){
		if (bid == null || bid.length() == 0) {
			return W.error(model, "单号不能为空");
		}
		
		TccDistribute entity = new TccDistribute().setBid(bid).queryFirst();
		ThrOperateCompanyApply thrOperateCompanyApply = new ThrOperateCompanyApply().setVgsbm(entity.getVgsbm()).queryFirst();
		YsProcInst ysProcInst = new YsProcInst().setBid(bid).queryFirst();
		Map<String, Object> extraParam = new HashMap<String, Object>();
		extraParam.put("vgsjc", thrOperateCompanyApply.getVgsjc());
		extraParam.put("vgsmc", thrOperateCompanyApply.getVgsmc());
		extraParam.put("auditTime", ysProcInst.getUpdateTime());
		try {

			// 子列表配置信息
			List<Map<String, Object>> sonlist = tccDistributeService.getItemList(bid);
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCC_Distribute, sonlist);
			
			return commonApiService.toEdit(model, entity.getId(), new TccDistribute(), MenuId.TCC_Distribute, extraParam, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
}